package com.aiyige.page.my.sell;

import android.app.Activity;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.api.ApiManager;
import com.aiyige.base.api.ResponseCode;
import com.aiyige.model.User;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.ErrorUtil;
import com.aiyige.utils.RetrofitUtil;
import com.aiyige.utils.SafeAsyncTask;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

@Route(path = ARouterConfig.MySellPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class MySellPage extends AppCompatActivity {
    @BindView(R.id.backBtn)
    ImageView backBtn;
    @BindView(R.id.mySellLabel)
    TextView mySellLabel;
    @BindView(R.id.salesVolumeTv)
    TextView salesVolumeTv;
    @BindView(R.id.orderNumTv)
    TextView orderNumTv;
    @BindView(R.id.orderNumLayout)
    LinearLayout orderNumLayout;
    @BindView(R.id.intentionCustomerNumTv)
    TextView intentionCustomerNumTv;
    @BindView(R.id.intentionCustomerLayout)
    LinearLayout intentionCustomerLayout;
    @BindView(R.id.headerLayout)
    ConstraintLayout headerLayout;
    @BindView(R.id.orderManagementLayout)
    LinearLayout orderManagementLayout;
    @BindView(R.id.merchandisemanagementLayout)
    LinearLayout merchandisemanagementLayout;
    @BindView(R.id.customerManagementLayout)
    LinearLayout customerManagementLayout;
    @BindView(R.id.waitingReceiveMoneyLayout)
    LinearLayout waitingReceiveMoneyLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_my_sell);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        requestData();
    }

    public void requestData() {
        new RequestDataTask(this).execute();
    }

    public class RequestDataTask extends SafeAsyncTask<Object, Object, Object> {

        public RequestDataTask(Activity activity) {
            super(activity);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            updateView();
        }

        @Override
        protected Object doInBackground(Object... objects) {
            try {
                Response<ResponseBody> response = ApiManager.getService()
                        .userCenterUsers(AccountUtil.getCurrentUser().getId())
                        .execute();
                if (response.code() != ResponseCode.OK) {
                    throw new Exception(ErrorUtil.extractMessageFromErrorResponse(response));
                }
                String json = RetrofitUtil.getResponseBodyAsString(response);
                User tempUser = JSON.parseObject(json, User.class);
                User user = AccountUtil.getCurrentUser();
                user.update(tempUser);
                AccountUtil.updateCurrentUser(user);
                return null;
            } catch (Exception e) {
                return e;
            }
        }

        @Override
        public void safeOnPostExecute(Object o) {
            if (o instanceof Exception) {
                ToastX.show((Exception) o);
            } else {
                updateView();
            }
        }


    }

    public void updateView() {
        salesVolumeTv.setText(StringUtils.priceFormat(AccountUtil.getCurrentUser().getStatisticsBackup().getTransactionAmount()));
        orderNumTv.setText(StringUtils.formatNum(AccountUtil.getCurrentUser().getStatisticsBackup().getSellCount()));
        intentionCustomerNumTv.setText(StringUtils.formatNum(AccountUtil.getCurrentUser().getStatisticsBackup().getInterestCount()));
    }

    @OnClick({R.id.backBtn, R.id.orderManagementLayout, R.id.merchandisemanagementLayout, R.id.customerManagementLayout, R.id.waitingReceiveMoneyLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backBtn:
                finish();
                break;
            case R.id.orderManagementLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.OrderManagerPage)
                        .navigation();
                break;
            case R.id.merchandisemanagementLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.MMPage)
                        .navigation();
                break;
            case R.id.customerManagementLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.CustomerManagementListPage)
                        .navigation();
                break;
            case R.id.waitingReceiveMoneyLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.WaitReceiveMoneyListPage)
                        .navigation();
                break;
        }
    }
}
